{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cdb34ff5-9b0c-4ddb-b8f5-1390536dbbd8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import dashscope\n",
    "import os\n",
    "import json\n",
    "import requests\n",
    "import jsonlines\n",
    "from tqdm import tqdm\n",
    "import time\n",
    "from multiprocessing import Pool\n",
    "from functools import partial\n",
    "\n",
    "from tenacity import (\n",
    "    retry,\n",
    "    stop_after_attempt,\n",
    "    wait_random_exponential,\n",
    "    RetryError\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "27bdbe4e-e6bd-4a68-a583-1e68773f0491",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = list(jsonlines.open(\"../data/wiki_roleplay_multilingual_test_input_w_evidence.jsonl\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4d8e7b0c-f716-475f-8d2c-7c2caf51afb5",
   "metadata": {},
   "outputs": [],
   "source": [
    "def call_qwen(messages):\n",
    "    try:\n",
    "        response = dashscope.Generation.call(\n",
    "            model='qwen-plus', # or qwen-max\n",
    "            presense_penalty=0,\n",
    "            messages=messages,\n",
    "            result_format='message',\n",
    "            headers={\"X-DashScope-DataInspection\": \"disable\"}\n",
    "        )\n",
    "        assert response is not None and response.output is not None\n",
    "        return response\n",
    "    except Exception as e:\n",
    "        pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d3e8b5d5-48a9-456b-be03-fa708677333b",
   "metadata": {},
   "outputs": [],
   "source": [
    "@retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(5))\n",
    "def process(item):\n",
    "    dashscope.api_key = \"your api key\"\n",
    "    messages = [{'role': 'system', 'content': item['system']}]\n",
    "    for prompt in item['prompts']:\n",
    "        messages.append({\"role\": \"user\", \"content\": prompt})\n",
    "        response = call_qwen(messages)\n",
    "        messages.append(dict(response.output.choices[0].message))\n",
    "        time.sleep(1/60)\n",
    "    item['messages'] = messages\n",
    "    return item"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c6d6d0bb-09b7-451e-9876-1561d93740c1",
   "metadata": {},
   "outputs": [],
   "source": [
    "results = []\n",
    "pbar = tqdm(total=len(data))\n",
    "for item in data:\n",
    "    results.append(process(item))\n",
    "    pbar.update(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "240cf8dc-7389-45e3-ac3d-f88c6a2d9a80",
   "metadata": {},
   "outputs": [],
   "source": [
    "with jsonlines.open(f\"../data/results/wiki_roleplay_multilingual_test_input_w_evidence_qwen-plus_output.jsonl\", \"w\") as f:\n",
    "    for each in results:\n",
    "        f.write(each)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
